// Copyright (c) 2022 CHANGLEI. All rights reserved.

import 'package:aircraft_war/bloc/game_state_cubit.dart';
import 'package:aircraft_war/components/alive_value.dart';
import 'package:aircraft_war/components/firepower_value.dart';
import 'package:aircraft_war/components/pause_button.dart';
import 'package:flame/components.dart';

/// Created by changlei on 2022/5/23.
///
/// 游戏时的操作按钮和生命值火力值显示
class GameRunning extends GameStatePositionComponent {
  late final _pauseButton = PauseButton();
  late final _aliveValue = AliveValue();
  late final _firepowerValue = FirepowerValue();

  /// 布局
  void layout(Vector2 parentSize) {
    size = parentSize;
    position = Vector2.zero();

    _pauseButton.layout(size);
    _aliveValue.layout(size);
    _firepowerValue.layout(size);
  }

  @override
  Future<void> onLoad() async {
    onNewState(gameRef.stateCubit.state);
  }

  @override
  void onNewState(GameState state) {
    switch (state) {
      case GameState.idle:
      case GameState.pending:
      case GameState.stopped:
        _pauseButton.removeFromParent();
        _aliveValue.removeFromParent();
        _firepowerValue.removeFromParent();
        break;
      case GameState.running:
      case GameState.pausing:
        if (_pauseButton.parent == null) {
          _pauseButton.addToParent(this);
        }
        if (_aliveValue.parent == null) {
          _aliveValue.addToParent(this);
        }
        if (_firepowerValue.parent == null) {
          _firepowerValue.addToParent(this);
        }
        break;
    }
  }
}
